package com.zw.pdm.module.est.service;

import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.entity.est.CardDetail;
import com.zw.pdm.entity.est.CraftRoute;
import com.zw.pdm.module.est.mapper.CardDetailMapper;
import com.zw.pdm.module.est.mapper.CardMapper;
import com.zw.pdm.module.est.model.CardModel;
import com.zw.pdm.module.est.repository.CardRepository;
import com.zw.pdm.module.est.repository.CraftRouteRepository;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @Description:
 *
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class CardVerifyService {

    @Autowired
    private CardRepository cardRepository;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CardDetailMapper cardDetailMapper;
    @Autowired
    private LogService logService;
    @Autowired
    private CraftRouteRepository craftRouteRepository;

    /** 列表 */
    public List<CraMakeSheet> findWithCardVerify(CraMakeSheet craMakeSheet) {
        return cardMapper.findWithCardVerify(craMakeSheet);
    }

    /** 子列表查询 */
    public List<CardModel> findSlaveWithCardVerify(CardModel card) {
        return cardMapper.findSlaveWithCardVerify(card);
    }

    /** 按主键查询 */
    public Card getCardById(Integer id) {
        return cardRepository.getOne(id);
    }

    /** 查询工艺卡明细 */
    public List<CardDetail> findCardDetailByCardId(Integer id) {
        return cardDetailMapper.findByCardId(id);
    }

    /** 校对通过 */
    @Transactional(rollbackFor = Exception.class)
    public int success(Integer id) {
        logService.saveLog("校对工艺卡", "工艺卡ID：" + id);
        // 生成工艺路线表数据
        CraftRoute route = new CraftRoute();
        route.setCardId(id);
        route.setIsDel(0);
        route.setRoute(genRoute(id));
        craftRouteRepository.save(route);
        return cardMapper.updateIsCheckAndVerifyTimeById(id, 1, new Date());
    }

    private String genRoute(Integer id) {
        List<CardDetail> details = cardDetailMapper.findByGroupByOrderNoCardId(id);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < details.size(); i++) {
            String r = details.get(i).getRoute();
            int l = r.length();
            switch (l) {
                case 1:
                    r += "&emsp;&emsp;&emsp;";
                    break;
                case 2:
                    r += "&emsp;&emsp;";
                    break;
                case 3:
                    r += "&emsp;";
                    break;
            }
            if (i + 1 < 10) {
                sb.append("&nbsp;&nbsp;" + (i + 1) + "、" + r);
            } else {
                sb.append((i + 1) + "、" + r);
            }
        }
        return sb.toString();
    }

    /** 校对驳回 */
    @Transactional(rollbackFor = Exception.class)
    public int reject(Card card) {
        logService.saveLog("驳回工艺卡", "工艺卡ID：" + card.getId());
        cardMapper.delCraftRouteByCardId(card.getId());
        cardMapper.updateIsCheckAndVerifyTimeById(card.getId(), 0, null);
        return cardMapper.updateStatusAndReject(card);
    }

    /** 判断是否可以转定额 */
    public boolean preQuota(Integer id) {
        int row = cardMapper.countCraMakeDetailBySheetId(id);
        int row2 = cardMapper.countCardByCraMakeSheetId(id);
        return row == row2;
    }

    /** 转定额 */
    @Transactional(rollbackFor = Exception.class)
    public int quota(Integer id) {
        logService.saveLog("转定额", "工艺图纸目录ID：" + id);
        cardMapper.updateMakeSheetIsQuotaAndTurnTime(id, 3, new Date());
        return cardMapper.updateStatusAndTurnTimeByCraMakeSheetId(id, 3, new Date());
    }

    /** 新转定额 */
    public int newQuota(Integer id) {
        logService.saveLog("转定额", "工艺卡ID：" + id);
        cardMapper.updateCraMakeSheetChangeCommitByCardId(id);
        cardMapper.updateReleaseDetailIsSuccessByCardId(id);
        return cardMapper.updateStatusAndTurnTimeById(id, 3, new Date());
    }

    /** 撤消转定额 */
    @Transactional(rollbackFor = Exception.class)
    public int cancelQuota(Integer id) {
        logService.saveLog("撤消转定额", "工艺卡ID：" + id);
        cardMapper.updateIsCheckAndVerifyTimeById(id, 0, null);
        cardMapper.updateStatusById(id, 1);
        cardMapper.delCraftRouteByCardId(id);
        Integer makeSheetId = cardMapper.getMakeSheetIdByCardId(id);
        Integer count = cardMapper.countStatusByMakeSheetId(makeSheetId, 3);
        if (count > 0) {
            cardMapper.updateMakeSheetIsQuotaAndTurnTime(makeSheetId, 2, null);
        } else {
            cardMapper.updateMakeSheetIsQuotaAndTurnTime(makeSheetId, 1, null);
        }
        return 1;
    }

    /** 新列表 */
    public List<Card> findWithNewCardVerify(Card card) {
        card.setVerifyCode(ShiroUtils.getLoginName());
        return cardMapper.findWithNewCardVerify(card);
    }

    /** 批量转定额 */
    @Transactional(rollbackFor = Exception.class)
    public int newQuotaAll(Integer[] ids) {
        cardMapper.updateCraMakeSheetNewChangeCommitByCardId(ids);
        cardMapper.updateStatusAndTurnTimeByIds(ids, 3, new Date());
        return 1;
    }

    /** 撤消转定额 */
    @Transactional(rollbackFor = Exception.class)
    public int newCancelQuota(Integer id) {
        logService.saveLog("撤消转定额", "工艺卡ID：" + id);
        cardMapper.updateIsCheckAndVerifyTimeById(id, 0, null);
        cardMapper.updateStatusById(id, 1);
        return cardMapper.delCraftRouteByCardId(id);
    }
}
